const { DataTypes } = require("sequelize");
const sequelize = require("../config/sequelize");
const User = require("./user");

const Score = sequelize.define(
  "Score",
  {
    content:{
        type: DataTypes.STRING,
        allowNull:false,
        comment: '积分明细内容'
    },
    num:{
        type:DataTypes.INTEGER,
        allowNull:false,
        comment: '积分明细'
    }
  },
  {
    sequelize:sequelize,
    tableName:'score',
    underscored:true
  }
);
Score.belongsTo(User,{
    constraints:false,
    foreignKey:'userId',
    as:'user'
});

Score.afterCreate(async (score) => {
    // 用户积分数更改
    await User.increment({score: score.num}, {where: {id: score.userId}});
});
module.exports = Score;
